package com.nbk.composition.controller

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.nbk.composition.bean.Response
import com.nbk.composition.dao.bean.Composition
import com.nbk.composition.dao.bean.Order
import com.nbk.composition.dao.bean.Student
import com.nbk.composition.dao.repository.CompositionMapper
import com.nbk.composition.dao.repository.OrderMapper
import com.nbk.composition.dao.repository.StudentMapper
import com.nbk.composition.error
import com.nbk.composition.manager.TeacherAllot
import com.nbk.composition.utils.createBatchNumber
import com.nbk.composition.utils.getCurrentTime
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

/**
 * Author:  LuoHaoLun
 * Email :  506503279@qq.com
 * Date  :  2021/5/25
 */
@RestController
@RequestMapping("/api/")
class StudentController {

    @Autowired
    private lateinit var studentMapper: StudentMapper

    @Autowired
    private lateinit var compositionMapper: CompositionMapper

    @Autowired
    private lateinit var orderMapper: OrderMapper

    @GetMapping("getStudent")
    fun getStudent(device: String): Response {
        if (device.isEmpty()) "设备码为空".error()
        val queryWrapper = KtQueryWrapper(Student::class.java)
        queryWrapper.eq(Student::device, device).last("limit 1")
        val student = studentMapper.selectOne(queryWrapper)
        return Response().apply { data = student }
    }

    @PostMapping("registry")
    fun registry(@RequestBody student: Student): Response {
        // TODO 懒得做判断了
        student.createTime = getCurrentTime()
        studentMapper.insert(student)
        return Response().apply { data = student }
    }

    @PostMapping("upload")
    fun upload(@RequestBody composition: Composition): Response {
        if (composition.isAmendOrSendBack == true) {
            val queryComposition = KtQueryWrapper(Composition::class.java).apply {
                eq(Composition::orderId, composition.orderId)
            }
            val count = compositionMapper.selectCount(queryComposition)

            val order = orderMapper.selectById(composition.orderId) ?: "找不到订单".error()
            order.residueCount--
            if (order.residueCount < 0) order.residueCount = 0
            orderMapper.updateById(order)

            composition.id = -1
            composition.seq = count + 1
            composition.uploadTime = getCurrentTime()
            composition.state = "待批改"
            composition.correct.clear()
            composition.score = 0
            composition.residueCount = order.residueCount
            composition.comment = null
            composition.correctTime = ""
            compositionMapper.insert(composition)
            TeacherAllot.add(composition.teacherId)
        } else {
            compositionMapper.selectById(composition.id)?.let { // 未支付前都可以修改
                it.content = composition.content
                it.uploadTime = getCurrentTime()
                compositionMapper.updateById(it)
            } ?: kotlin.run {                                  // 首次提交
                composition.seq = 1
                composition.uploadTime = getCurrentTime()
                compositionMapper.insert(composition)
            }
        }
        return Response().apply { data = composition.id }
    }

    @PostMapping("pay")
    fun pay(@RequestBody order: Order): Response {
        compositionMapper.selectById(order.compositionId)?.let {
            if (it.state != "待支付") "已经支付过了".error()
            val teacherId = TeacherAllot.allot()
            order.teacherId = teacherId
            order.batchNumber = createBatchNumber()
            order.payTime = getCurrentTime()
            orderMapper.insert(order)

            it.teacherId = teacherId
            it.orderId = order.id
            it.state = "待批改"
            it.batchNumber = order.batchNumber
            it.payType = order.payType
            it.residueCount = order.residueCount
            compositionMapper.updateById(it)
        } ?: "查询不到此作文订单，请重新提交".error()
        return Response().apply { data = order.batchNumber }
    }


    @GetMapping("getAllComposition")
    fun getAllComposition(studentId: Int): Response {
        val queryWrapper = KtQueryWrapper(Composition::class.java)
        queryWrapper.eq(Composition::studentId, studentId).orderByDesc(Composition::id)
        val compositions = compositionMapper.selectList(queryWrapper)
        return Response().apply { data = compositions }
    }

    @GetMapping("getReportList")
    fun getReportList(studentId: Int): Response {
        val queryWrapper = KtQueryWrapper(Composition::class.java)
        queryWrapper.eq(Composition::studentId, studentId).and {
            it.eq(Composition::state, "待评价").or().eq(Composition::state, "已评价")
        }.orderByDesc(Composition::id)
        val compositions = compositionMapper.selectList(queryWrapper)
        return Response().apply { data = compositions }
    }

}